import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 生成原文文件头的每条文本的索引，修改文本扩容用。破解阶段调试代码，已整合进WhoEncode1001.java
 * */
public class WhoExtend1001 {
	
	/** 文件内容 */
	static byte[] bFile = null;
	
	public static void main(String ar[]) {
		//读上次路径
		
		JFileChooser file = new JFileChooser("d:/我的文档/桌面/1001_new/decoded");
		file.setMultiSelectionEnabled(true);
        FileFilter filter = new FileNameExtensionFilter("bin Files(*.bin)", "bin");
        file.addChoosableFileFilter(filter);
        file.setFileFilter(filter);
		file.showOpenDialog(null);
		File fl[] = file.getSelectedFiles();
		if (fl.length == 0) {
			System.exit(0);
		}

		FileInputStream fin = null;
		ByteArrayInputStream bais = null;
		DataInputStream dis = null;
		FileOutputStream fileout = null;
		
		for (int file_i=0; file_i<fl.length; file_i++) {
			
			try {
				
				RandomAccessFile raf = new RandomAccessFile(fl[file_i], "rw");
				
				/** 文件中有几条文本 */
				short textCount = convShort(raf.readUnsignedShort());
				/** 文本索引 */
				int[] textIndex = new int[textCount];
				/** 文本索引数组的下标 */
				int p = 0;
				
				
//				int[] t=new int[textCount];
//				for (int i=0; i<textCount; i++) {
//					t[i]=convInt(raf.readInt());
//				}
//				prtArray(t);
				raf.skipBytes(textCount * 4);
				
				textIndex[p++] = (int)raf.getFilePointer();
				short s0 = 0, s1 = 0, s2 = 0;
				int count_textArray = 0;
				for (int i=(int)raf.getFilePointer(); i<raf.length()-10; i+=2) {
					s0 = raf.readShort();
					if (s0 == 0x0500) {
						i+=2;
						if(raf.readShort() == 0x0500) {
							i+=2;
							s2 = raf.readShort();
							if(s2 == 0x0500) {	//05 00 05 00 05 00
								if(count_textArray>0) {
									count_textArray--;
								} else {
									textIndex[p++] = (int)raf.getFilePointer();
									if (p==textCount) break;
								}
							} else if (s2 == 0x0400) {	
								//05 00 05 00 04 00 02 00 ,"554",下边是数组,长度为2,
								//有好多层的,每出现一个"554",就跳过一(即2--)个"555"
								i+=2;
								count_textArray += convShort(raf.readShort());
								count_textArray--;
							}
						}
					} else if (s0 == 0x0100) {
						i+=2;
						s1 = raf.readShort();
						if (s1 == 0x5300) {
							i+=2;
							s2 = raf.readShort();
							if (s2 == 0x0200) {	//01 00 53 00 02 00 CA 00 25 00 unicode数字
								//unicode数字 个文本数组,如33 00 30 00表示要跳过30个"555" 
								i+=4;
								raf.skipBytes(4);
								int num=0;
								i+=2;
								num = convShort(raf.readUnsignedShort()) - 0x30;
								while (num >= 0 && num <= 9) {
									count_textArray = count_textArray * 10 + num;
									i+=2;
									num = convShort(raf.readUnsignedShort()) - 0x30;
								}
								count_textArray--;
							}
						}
						
					}
				}
//				System.out.println("========"+fl[file_i].getName());
//				for(int i=0;i<textCount;i++) {
//					if (t[i] != textIndex[i]) {
//						System.out.println(i+" i, idx "+textIndex[i]);
//					}
//				}
				
				//写入覆盖原索引
				for(int i=2,j=0; i<textIndex[0]; i+=4,j++) {
					raf.seek(i);
					raf.writeInt(convInt(textIndex[j]));
				}
				raf.close();
				
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (dis != null) dis.close();
					if (fin != null) fin.close();
					if (fileout != null) fileout.close();
				} catch (IOException e) {
						e.printStackTrace();
				}
			}
		}
	}
	
	public static int convInt(int i) {
		int result = 0;
		result = (i & 0xff000000) >>> 24 | (i & 0x00ff0000) >> 8 | 
				(i & 0x0000ff00) << 8 | (i & 0x000000ff) << 24;
		return result;
	}
	public static short convShort(int i) {
		int result = 0;
		result = (i & 0xff00) >>> 8 | (i & 0x00ff) << 8;
		return (short)result;
	}

	private static void prtArray(int[] b) {
//		//debug to print fileHead
		int prntmp=0;
		for (int i=0; i<b.length; i++) {
			System.out.print(b[i] + ", ");
			prntmp++;
			if (prntmp == 16) {
				prntmp = 0;
				System.out.println();
			}
		}
	}
}
